package com.baosight.sczx.zm.cg.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHWorkReport;

/**
 * <p>Title: ServiceZMCG82.java</p>
 * <p>Description: 抽采进度日汇报(调度日报用)</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-25 下午12:14:15  张小飞  Create
 *</pre>
 */
public class ServiceZMCG82 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCG82.java</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:45 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
	
		EiInfo outInfo = this.query(inInfo);
//		inInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
//		inInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
//		inInfo.set("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
//		inInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
//		inInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
//		inInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
//		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));
//		outInfo.addBlock(StaticBlocks.getStauts4());
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCG82</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:59 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
//		String fdDepartmentId = UserSession.getInSessionProperty("orgCode").toString();//部门
//		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);//添加查询条件
		
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCHWorkReport.query", new ZMCHWorkReport());
		
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));//班次
		outInfo.addBlock(StaticBlocks.getStauts4());//状态
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false)); //队别
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(Engineering.getEngineeringForJJ("cmes.cp.engineering", "1", "", "1", false,UserSession.getInSessionProperty("miningCode").toString()));//工程名称
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ch.workPlanId", false));//工作安排
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ch.projectId", false));//工作面名称
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));
		
//		outInfo.set("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值	
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		

		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCG82</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:31:06 张小飞
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		
		EiInfo out=new EiInfo(); 
		ZMCHWorkReport _dom = new ZMCHWorkReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			//校验是否重复
			Map<String, String>	params = new HashMap<String, String>();
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_WORKREPORT"));
			this.dao.insert("ZMCHWorkReport.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCG82</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:59:23 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {		
		EiInfo out=new EiInfo();
		ZMCHWorkReport _dom = new ZMCHWorkReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

		    this.dao.update("ZMCHWorkReport.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCG82</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:12:10 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
        
		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{

			EiInfo _temp = super.delete(inInfo, "ZMCHWorkReport.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}	
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCHWorkReport _dom = new ZMCHWorkReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			this.dao.update("ZMCHWorkReport.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}


}
